Technote 1xxxNative Scripting Additions**Preliminary draftBy Andy BachorskiApple Worldwide Developer Technical Support |
CONTENTS
|
This Technote describes the native
scripting additions mechanism for AppleScript introduced in Mac OS 8.6.
The native scripting additions mechanism described here addresses limitations in the classic code-resource-based mechanism, and provides a way to package the handlers in a scripting addition as a single shared library. This packaging allows the sharing of code and the ability to maintain persistent state between calls. This Technote is directed at application developers who create scripting additions. |
Classic Scripting AdditionsAppleScript provides a mechanism for extending the syntax of the language
through the use of scripting additions (also called osaxen). This classic
scripting addition mechanism was designed in the days when the Mac OS ran
only on 68K machines, and so is based on code resources. These code resources
are of type At startup time, AppleScript installs an event handler in the system
dispatch table, which is responsible for loading scripting additions and
gathering their terminologies. This handler scans the Scripting Additions
folder looking for files of type
The The handler in the Being resource-based, the classic scripting addition mechanism has a
number of limitations which restrict how scripting additions can be written
and what they can do. Since |
Accelerated Scripting AdditionIn the classic scripting addition mechanism, the only way to create a native scripting addition is to package it as an accelerated code resource (ACR). This method has its own complications and does not address any of the limitations of the classic mechanism. Because of the complications accelerated code resource cause, we strongly discourage anyone from writing an ACR-based scripting addition. The problem with ACRs is caused by
the way AppleScript loads and prepares It is this lack of concern for what happens when an |
Native Scripting AdditionsA native scripting addition, as the name implies, contains PowerPC code.
This code must be in a single PEF container in the data fork of the scripting
addition file. The packaging of the executable code is the only difference
between a native and classic scripting addition file (other than the Whether native or classic, scripting addition files will contain resources
of other types. A terminology resource (type
|
Runtime EnvironmentA native scripting addition needs to provide routines for: CFM InitializationA native scripting addition needs to define a CFM initialization routine for the code fragment containing the scripting addition's handlers. This initialization routine should check to ensure that any needed services and system resources are available before installing any handlers. If requirements are met, the scripting addition should install its handlers. The scripting addition's handlers must be installed in the system dispatch table.
In the classic scripting addition model, a flag bit in the
The scripting addition's initialization routine should save the
Event and Coercion HandlingScripting additions are a packaging mechanism for event and coercion handlers used to extend the basic capabilities of AppleScript. These handlers are written in basically the same way that handlers used inside an application would be written. What differs between scripting additions and applications is the packaging of the handlers. In the classic scripting addition model, handlers need to be a self-
contained With native scripting additions, duplication of code is no longer a problem. When a native scripting addition contains more than one handler, those handlers can share common code. Native scripting additions are packaged as a single code fragment containing all handlers, which means there is no need to duplicate code. It is often possible with the native scripting addition model to install the same handler for multiple events or coercions, and vary the behavior as needed based on the input parameters.
In the classic scripting addition model, a flag bit in the For native scripting additions, each handler is responsible for detecting and rejecting
events from remote systems (if appropriate). A handler can determine the source
of an event by examining the
If the native scripting addition needs to open its resource fork, it must
open it after a handler has been called, and close it before the handler
exits. It should also have save the
CFM TerminationA native scripting addition needs to define a CFM termination routine for the code fragment containing the scripting addition's handlers. The scripting addition's termination routine is called when AppleScript closes its connection to the scripting addition. This closure will happen when the system is shutdown or restarted. It will also happen when the scripting addition is removed from the Scripting Additions folder by the user. When the termination routine is called, it must perform any actions needed to close down the scripting addition. One very important termination action is to uninstall any handlers, either event or coercion, that the scripting addition has installed. Failure to do this will most likely result in a crash when a script attempts to call a handler in the scripting addition, since the handler will no longer be available.
|
Thanks to Chris Espinosa, Steve Evangelou, Ingrid Kelly, Chris Nebel, and Quinn "The Eskimo!"